import { createRouter, createWebHistory } from "vue-router"
import { routes } from "./routes"

const router = createRouter({
    history: createWebHistory(),
    routes
})
//配置路由守卫
router.beforeEach((to, from, next) =>{
	// return false
	// 如何判断用户是否登录。 user-store.name
	const user_data_str = localStorage.getItem("s_user")
	var user_json = null
	if(user_data_str){
		user_json = JSON.parse(user_data_str)["name"]
	}
	console.log(user_json,'user')
	if(to.meta.needLogin && !user_json){
	// if(to.meta.needLogin){
		next({ name: 'login' })
	} else next()
})

export default router